/**
 * param success 数据库连接成功的回调
 * param error 数据库连接失败的回调
 */
module.exports = function(success,error){

    //当第二个参数不传递时，则给error个默认值
    if(typeof error !== 'function'){
        error = ()=>{
            console.log('连接失败');
        }        
    }

//1、安装mongoose     npm i mongoose
//2、导入mongoose
const mongoose = require('mongoose');

//导入数据库配置模块
const {dburl,dbport,db} = require('./config'); 

//3、连接mongo服务
mongoose.connect(`mongodb://${dburl}:${dbport}/${db}`);

//4、设置回调  设置连接成功的回调
// mongoose.connection.on('open',()=>{
//     console.log("连接成功");
// });

mongoose.connection.once('open',()=>{
    success();
    console.log("连接成功");
});

//设置连接错误的回调
mongoose.connection.on('error',()=>{
    error();
    console.log("连接失败");
});

//设置连接关闭的回调
mongoose.connection.on('close',()=>{
    console.log("连接关闭");
});
}